﻿using OkFlurl;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using VictoriaLogs.Client;

namespace Test
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            //https://docs.victoriametrics.com/victorialogs/data-ingestion/#json-stream-api
            //https://docs.victoriametrics.com/victorialogs/querying/

            //client must be singleton mode(必须是单例模式)
            var client = new VictoriaLogsClient("http://127.0.0.1:9428");
            
            //insert list
            var list = new List<object>();
            for (int i = 0; i < 10; i++)
            {
                var item = new
                {
                    _time = DateTime.Now,
                    _msg = "hello" + i,
                    app = "qms",
                    level = "info",
                    money = i
                };

                list.Add(item);

                Thread.Sleep(1);
            }
            await client.InsertListAsync(list, ["app"]);

            //insert by your self
            await client.InsertAsync(async stream =>
            {
                var model = new
                {
                    app = "qms",
                    _msg = "object model",
                    _time = DateTime.Now
                };
                await JsonSerializer.SerializeAsync(stream, model, VictoriaLogsClient.JsonOptionsMs);

                await stream.WriteAsync(VictoriaLogsClient.N); //write an \n

                var jsonText = """
                {"app":"qms","_msg":"json line"}
                """;
                await stream.WriteAsync(Encoding.UTF8.GetBytes(jsonText));

            }, ["app"]);


            //query
            //var query = "*"; //query all
            var query = """
                {app="qms"} | order by (_time desc) | limit 2
                """;
            var unbuffer = client.QueryAsync(query);
            await foreach (var item in unbuffer)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("===================");

            //query streams
            var streamsData = await client.QueryOtherAsync<JsonObject>(QueryType.Streams, "*");
            Console.WriteLine(JsonSerializer.Serialize(streamsData, FlurlExt.JsonOptions));

            //queue
            var queue = new VictoriaLogsQueue(client);

            queue.OnError += (errList) =>
            {
                Console.WriteLine($"error:{errList.Count}");
            };

            _ = Task.Run(async () =>
            {
                while (true)
                {
                    var obj = new People()
                    {
                        _time = DateTime.Now,
                        _msg = "哈哈哈" + DateTime.Now.Second,
                        app = "wms",
                        host = "127.0.0.1",
                        Name = "李四",
                        Remark = JsonSerializer.Serialize(new { id = 1, name = "厦门", money = 11.2 }, VictoriaLogsClient.JsonOptionsMs)
                    };

                    queue.Add(obj, ["app"]);
                    queue.Add(obj, ["app", "host"]);
                    queue.Add(obj, ["host", "app"]);

                    var obj2 = new
                    {
                        _time = DateTime.Now,
                        _msg = "哈哈哈" + DateTime.Now.Second,
                        pod = "nasa"
                    };

                    queue.Add(obj2, ["pod"]);

                    await Task.Delay(500);
                }

            });

            Console.WriteLine("Hello, World!");
            Console.ReadKey();
        }
    }
}
